﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.Attendance
{
    public class AttendanceDbContext : DbContext
    {
        public AttendanceDbContext()
            : base("name=" + SmartSpace.Configuration.ConnectionStringName["attendance"])
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<AttendanceDbContext>(new MigrateDatabaseToLatestVersion<AttendanceDbContext, AutomaticDbMigrationsConfiguration<AttendanceDbContext>>());
            }
            else
            {
                Database.SetInitializer<AttendanceDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Record>().ToTable("attendance_record");
            modelBuilder.Entity<Record>().HasKey(p => p.Id);
            base.OnModelCreating(modelBuilder);
        }
    }

    public sealed class AutomaticDbMigrationsConfiguration<TContext> : DbMigrationsConfiguration<TContext> where TContext : System.Data.Entity.DbContext
    {
        public AutomaticDbMigrationsConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}
